<template>
  <div class="app-container customer-div" style="overflow-x: auto">
    <!-- tabs标签页  -->
    <el-tabs v-model="listQuery.tab" type="card" @tab-click="handleClick1">
      <el-tab-pane label="统计" name="statistics"></el-tab-pane>
      <el-tab-pane label="按品类" name="goodsName"></el-tab-pane>
    </el-tabs>
    <!-- 搜索栏  -->
    <el-form :inline="true" :model="listQuery" size="mini" label-width="50px">
      <el-form-item label="日期：">
        <el-date-picker
          v-model="listQuery.inquiryDate"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="品名:"
        label-width="50px"
        v-if="listQuery.tab == 'goodsName'"
      >
        <el-select
          clearable
          v-model="listQuery.goodsName"
          filterable
          remote
          allow-create
          reserve-keyword
          placeholder="请输入关键词"
          :loading="optionsLoading"
          @focus="getGoodsSortList"
        >
          <el-option
            v-for="item in options"
            :key="item.categoryName"
            :label="item.categoryName"
            :value="item.categoryName"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label=""
        label-width="0px"
        v-if="listQuery.tab == 'goodsName'"
      >
        <el-radio-group v-model="listQuery.type">
          <el-radio-button label="total">按合计</el-radio-button>
          <el-radio-button label="day">按日</el-radio-button>
          <el-radio-button label="month">按月</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button
          style="margin-left: 10px"
          type="primary"
          @click="getTruckRatioReport"
        >
          查询
        </el-button>
        <el-button
          :loading="btnLoading"
          style="margin-left: 10px"
          @click="downloadExcel"
          type="primary"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <!-- echarts图标 -->
    <div
      id="truckRatioChart"
      style="height: 400px; margin-top: 20px"
      v-loading="loadingChart"
    ></div>
    <!-- 数据列表 -->
    <el-table
      :data="tableData"
      style="width: 98%; margin-top: 20px"
      border
      height="500"
      :header-cell-style="{ background: '#fafafa' }"
    >
      <!-- <el-table-column
        v-if="listQuery.tab == 'statistics'"
        prop="inquiryDate"
        label="日期"
        min-width="120"
        align="center"
      >
      </el-table-column> -->
      <el-table-column
        v-if="listQuery.type == 'day'"
        prop="inquiryDate"
        label="日期"
        min-width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="listQuery.type == 'month'"
        prop="inquiryDate"
        label="日期"
        min-width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="listQuery.tab == 'goodsName'"
        prop="goodsName"
        label="品类"
        min-width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="inquiryBulkLoadCount"
        label="询价散货次数"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="inquiryCompleteLoadCount"
        label="询价整车次数"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="inquiryBulkLoadNum"
        label="询价散货数量/占比"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="inquiryCompleteLoadNum"
        label="询价整车数量/占比"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="orderBulkLoadCount"
        label="下单散货次数"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="orderCompleteLoadCount"
        label="下单整车次数"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="orderBulkLoadNum"
        label="下单散货数量/占比"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="orderCompleteLoadNum"
        label="下单整车数量/占比"
        min-width="100"
        align="center"
      >
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
const defaultListQuery = {
  inquiryDate: [], //搜索时间段 2022-10-11
  tab: "statistics", //tab项(统计:statistics,按品类:goodsName)
  type: "total", //分析类型(按合计:total, 按日:day,按月:month)
  goodsName: "", //品类
};
import { goodsSortList } from "@/api/allInLine/goods.js"; //导出
import { gettruckRatioReportAPI } from "@/api/allInLine/report.js";
import { mapGetters } from "vuex";
import { formatDate } from "@/utils/date";
import echarts from "echarts";
export default {
  name: "truckRatioReport",
  data() {
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "7天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "30天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      btnLoading: false,
      loadingChart: false,
      // 图表数据
      xList: [],
      yList: [],
      zList: [],
      // 表格数据
      tableData: [],
      options: [], //品名
      optionsLoading: false,
    };
  },
  computed: {
    ...mapGetters(["name", "mobile", "userId"]),
  },
  created() {
    let now = new Date();
    let today = formatDate(now, "yyyy-MM-dd");
    this.listQuery.inquiryDate = [today, today];
    this.getTruckRatioReport();
    // goodsSortList().then((res) => {
    //   this.options = res;
    // });
  },
  mounted() {
    this.initChart();
  },
  methods: {
     // 获取品名列表
    getGoodsSortList() {
      this.optionsLoading = true;
      goodsSortList().then((res) => {
        this.options = res;
        this.optionsLoading = false;
      });
    },
    // 配置图表
    initChart() {
      const chart = echarts.init(document.getElementById("truckRatioChart"));
      const option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["整车数量", "散货数量"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.xList,
        },
        yAxis: [
          {
            type: "value",
            name: "整车数量",
            position: "left",
          },
          {
            type: "value",
            name: "散货数量",
            position: "right",
          },
        ],
        series: [
          {
            name: "整车数量",
            type: "line",
            stack: "Total",
            data: this.yList,
            yAxisIndex: 0,
            // lineStyle: {
            //   color: "#005BEA",
            // },
          },
          {
            name: "散货数量",
            type: "line",
            stack: "Total",
            data: this.zList,
            yAxisIndex: 1,
            // lineStyle: {
            //   color: "#FFDD94",
            // },
          },
        ],
      };
      chart.setOption(option);
    },
    //获取询价统计列表
    async getTruckRatioReport() {
      this.loadingChart = true;
      const res = await gettruckRatioReportAPI(this.listQuery);
      this.tableData = res.list;
      this.xList = res.xList;
      this.yList = res.yList;
      this.zList = res.zList;
      this.initChart();
      this.loadingChart = false;
    },
    // tab栏切换
    handleClick1() {
      this.listQuery.goodsName = "";
      this.getTruckRatioReport();
    },
    //下载excle
    downloadExcel() {
      let params = JSON.parse(JSON.stringify(this.listQuery));
      params.isExport = 1;
      this.btnLoading = true;
      gettruckRatioReportAPI(params)
        .then((res) => {
          this.btnLoading = false;
          require.ensure([], () => {
            const { export_json_to_excel } = require("sheet/Export2Excel");
            const tHeader = res.title;
            const filterVal = res.columns;
            const list = res.list;
            const data = this.formatJson(filterVal, list);
            export_json_to_excel(tHeader, data, res.fileName);
          });
        })
        .catch(() => {
          this.btnLoading = false;
        });
    },
    //下载表格数据格式化
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]));
    },
  },
};
</script>

<style scoped lang="scss">
.el-card {
  width: 500px;
}
.el-card /deep/ .el-card__body {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 90px;
  .inquiryNumber {
    text-align: center;
    font-size: 18px;
    font-weight: 500;
  }
  .inquiryStr {
    font-size: 14px;
    color: #ccc;
  }
}
</style>
